package com.example.movie.ui.user.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.dueeeke.videocontroller.component.PrepareView
import com.example.movie.R
import com.example.movie.common.constants.AppConstants
import com.example.movie.ui.user.adapter.VideoRecyclerViewAdapter.VideoHolder
import com.example.movie.ui.user.listener.OnItemChildClickListener
import com.example.movie.ui.user.listener.OnItemClickListener
import com.example.movie.utils.VideoBean

class VideoRecyclerViewAdapter(private val videos: MutableList<VideoBean>) :
    RecyclerView.Adapter<VideoHolder>() {

    private var mOnItemChildClickListener: OnItemChildClickListener? = null
    private var mOnItemClickListener: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoHolder {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_swipe_video, parent, false)
        return VideoHolder(itemView)
    }

    override fun onBindViewHolder(holder: VideoHolder, position: Int) {
        val videoBean = videos[position]
        Glide
            .with(holder.mThumb.context)
            .load(AppConstants.STATIC_SOURCE_URL + videoBean.thumb)
            .placeholder(android.R.color.darker_gray)
            .into(holder.mThumb)
        holder.mTitle.text = videoBean.title
        holder.mPosition = position
    }

    override fun getItemCount(): Int {
        return videos.size
    }

    fun addData(videoList: List<VideoBean>?) {
        val size = videos.size
        videos.addAll(videoList!!)
        //使用此方法添加数据，使用notifyDataSetChanged会导致正在播放的视频中断
        notifyItemRangeChanged(size, videos.size)
    }

    inner class VideoHolder internal constructor(itemView: View) :
        RecyclerView.ViewHolder(itemView), View.OnClickListener {

        @JvmField
        var mPosition = 0
        @JvmField
        var mPlayerContainer: FrameLayout = itemView.findViewById(R.id.player_container)
        var mTitle: TextView = itemView.findViewById(R.id.tv_title)
        var mThumb: ImageView
        var mPrepareView: PrepareView = itemView.findViewById(R.id.prepare_view)

        override fun onClick(v: View) {
            if (v.id == R.id.player_container) {
                if (mOnItemChildClickListener != null) {
                    mOnItemChildClickListener!!.onItemChildClick(mPosition)
                }
            } else {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener!!.onItemClick(mPosition)
                }
            }
        }

        init {
            mThumb = mPrepareView.findViewById(R.id.thumb)
            if (mOnItemChildClickListener != null) {
                mPlayerContainer.setOnClickListener(this)
            }
            if (mOnItemClickListener != null) {
                itemView.setOnClickListener(this)
            }
            //通过tag将ViewHolder和itemView绑定
            itemView.tag = this
        }
    }

    fun setOnItemChildClickListener(onItemChildClickListener: OnItemChildClickListener?) {
        mOnItemChildClickListener = onItemChildClickListener
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnItemClickListener = onItemClickListener
    }
}